package com.zxx.mvpPractice.presenter;


import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.Log;

import com.google.common.base.Preconditions;
import com.zxx.mvpPractice.base.RxPresenter;
import com.zxx.mvpPractice.model.bean.RequestSmsBody;
import com.zxx.mvpPractice.model.bean.User;
import com.zxx.mvpPractice.model.net.RetrofitHelper;
import com.zxx.mvpPractice.model.net.bean.LoginRegisterReponse;
import com.zxx.mvpPractice.model.net.bean.SmsResponse;
import com.zxx.mvpPractice.model.net.bean.UpdateLoginResponse;
import com.zxx.mvpPractice.model.net.bean.VerifyResponse;
import com.zxx.mvpPractice.presenter.contract.LoginFragmentContract;
import com.zxx.mvpPractice.presenter.contract.MissPwdFragmentContract;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2016/12/17.
 */
public class MissPwdFragmentPresenter extends RxPresenter implements MissPwdFragmentContract.Presenter {
    MissPwdFragmentContract.View mView;


    private String smsId;//用户id

    public MissPwdFragmentPresenter(@NonNull MissPwdFragmentContract.View oneView) {
        mView = Preconditions.checkNotNull(oneView);
        mView.setPresenter(this);
    }


    @Override
    public void bombUserUpdate(final String username, String password1, final String password2, String smsCode) {
        if (TextUtils.isEmpty(username)) {
            mView.showMessage("手机号不能为空");
            return;
        }

        if (TextUtils.isEmpty(smsCode)) {
            mView.showMessage("验证码不能为空");
            return;
        }


        if (TextUtils.isEmpty(password1) || TextUtils.isEmpty(password2)) {
            mView.showMessage("密码不能为空");
            return;
        }

        if (!password1.equals(password2)) {
            mView.showMessage("两次输入密码不一致");
            return;
        }

        RetrofitHelper.getBombLoinApi().verifySms(smsCode, username)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<VerifyResponse>() {
                    @Override
                    public void onStart() {  //先显示对话框
                        mView.showProgressDialog();
                    }

                    @Override
                    public void onCompleted() {  //请求结束，对话框消失
                        mView.hideProgressDialog();
                    }

                    @Override
                    public void onError(Throwable e) {   //error时
                        Log.d("zxx", "showErrorMessage:" + e.getMessage());
                        mView.showMessage("验证失败");
                        mView.hideProgressDialog();
                    }

                    @Override
                    public void onNext(VerifyResponse msg) {
//                        mView.showBmob(msg);
                        mView.showMessage("验证成功");
                        Log.d("zxx", "onNext:" + msg.toString());
                        User updateUser = new User();
                        updateUser.setUsername(username);
                        updateUser.setPassword(password2);
                        RetrofitHelper.getBombLoinApi().updateUser(smsId, updateUser)
                                .subscribeOn(Schedulers.io())
                                .unsubscribeOn(Schedulers.io())
                                .observeOn(AndroidSchedulers.mainThread())
                                .subscribe(new Subscriber<UpdateLoginResponse>() {
                                    @Override
                                    public void onStart() {  //先显示对话框
                                        mView.showProgressDialog();
                                    }

                                    @Override
                                    public void onCompleted() {  //请求结束，对话框消失
                                        mView.hideProgressDialog();
                                    }

                                    @Override
                                    public void onError(Throwable e) {   //error时
                                        Log.d("zxx", "showErrorMessage:" + e.getMessage());
                                        mView.showMessage("更新失败");
                                        mView.hideProgressDialog();
                                    }

                                    @Override
                                    public void onNext(UpdateLoginResponse msg) {
//                        mView.showBmob(msg);
                                        mView.showMessage("更新成功");
                                        Log.d("zxx", "onNext:" + msg.toString());
                                    }
                                });
                    }
                });
    }

    @Override
    public void bombRequestSms(String phoneNumber) {

        if (TextUtils.isEmpty(phoneNumber)) {
            mView.showMessage("手机号不能为空");
            return;
        }

        RequestSmsBody requestSmsBody = new RequestSmsBody();
        requestSmsBody.setMobilePhoneNumber(phoneNumber);
        RetrofitHelper.getBombLoinApi().requestSmsCode(requestSmsBody)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<SmsResponse>() {
                    @Override
                    public void onStart() {  //先显示对话框
                        mView.showProgressDialog();
                    }

                    @Override
                    public void onCompleted() {  //请求结束，对话框消失
                        mView.hideProgressDialog();
                    }

                    @Override
                    public void onError(Throwable e) {   //error时
                        Log.d("zxx", "showErrorMessage:" + e.getMessage());
                        mView.showMessage("发送失败");
                        mView.hideProgressDialog();
                    }

                    @Override
                    public void onNext(SmsResponse msg) {
//                        mView.showBmob(msg);
                        mView.showMessage("发送成功");
                        smsId = msg.getSmsId();
                        Log.d("zxx", "onNext:" + msg.toString());
                    }
                });
    }

    @Override
    public void verifySms() {
        String smsCode = "";
        String phoneNumber = "";
        RetrofitHelper.getBombLoinApi().verifySms(smsCode, phoneNumber)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<VerifyResponse>() {
                    @Override
                    public void onStart() {  //先显示对话框
                        mView.showProgressDialog();
                    }

                    @Override
                    public void onCompleted() {  //请求结束，对话框消失
                        mView.hideProgressDialog();
                    }

                    @Override
                    public void onError(Throwable e) {   //error时
                        Log.d("zxx", "showErrorMessage:" + e.getMessage());
                        mView.showMessage("验证失败");
                        mView.hideProgressDialog();
                    }

                    @Override
                    public void onNext(VerifyResponse msg) {
//                        mView.showBmob(msg);
                        mView.showMessage("验证成功");
                        Log.d("zxx", "onNext:" + msg.toString());

                    }
                });
    }



}
